package com.app.business.service.uni;

import com.app.business.model.CarLot;
import com.app.business.service.HouseService;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.jpa.PageBean;
import com.app.system.jpa.Wrapper;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.StringUtils;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by ddz on 2021/11/30.
 * 手机端
 * 房源业务层
 *
 * @author lsh
 */
@SuppressWarnings(value = "ALL")
@Service
public class AppCarLotService extends JpaBaseDao<CarLot, Integer> {
    @Autowired
    private HouseService houseService;

    /**
     * 根据协议id获取已选择的车位list
     *
     * @param azProjectId
     * @return
     */
    public PageBean<CarLot> getCarLotList(HttpServletRequest request,String azProjectIdStr) {
        Integer carLotId = ParamUtils.getInt(request, "carLotId", 0);
        Integer pageSize = ParamUtils.getInt(request, "size", 15);
        Integer pageNo = ParamUtils.getInt(request, "num", 1);
        String keyword = ParamUtils.getString(request, "keyword", "");
        Integer carLotType = ParamUtils.getInt(request, "carLotType", 0);
        Integer houseStatus = ParamUtils.getInt(request, "carLotStatus", 0);

        StringBuffer hql = new StringBuffer(" c from CarLot c inner join fetch c.azProject ap where ap.id in " + azProjectIdStr );
        if (carLotId != 0) {
            hql.append(" and c.id = " + carLotId + "");
        }
        if (StringUtils.isNotEmpty(keyword)) {
            hql.append("and (c.houseNum like '%" + keyword + "%' ) ");
        }
        if (carLotType != 0) {
            hql.append(" and c.carLotType = " + carLotType + "");
        }
        if (houseStatus != 0) {
            hql.append(" and c.houseStatus = " + houseStatus + "");
        }
        StringBuffer orderBy = new StringBuffer(" c.houseStatus,c.chooseHouseDate,c.houseNum");
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setPageNum(pageNo);
        Wrapper wrapper = new Wrapper(pageBean, hql, null, orderBy);
        return this.pageHql(wrapper);
    }

    /**
     * 征收项目对应的可选车位数据
     * @param zsProjectId
     * @return
     */
    public List<CarLot> getCarLotListByZsProjectId(String azProjectIdStr,String carNum,Integer start,Integer pageSize) {
        StringBuffer hql = new StringBuffer("from CarLot c inner join fetch c.azProject ap where c.azProject.id in " + azProjectIdStr);
        if (carNum != "") {
            hql.append(" and c.houseNum like '%" + carNum + "%' ");
        }
        hql.append(" ORDER by ap.id,c.houseStatus,c.buildNum,c.floor,c.houseNum ");
        Map<String, Object> map = Maps.newLinkedHashMap();
        return this.findHqlLimit(hql.toString(), map,start,pageSize);
    }

    /**
     * 得到车位最大条数
     * @param azProjectIdStr
     * @param carNum
     * @return
     */
    public Integer getCarLotListByZsProjectIdSizw(String azProjectIdStr,String carNum) {
        StringBuffer hql = new StringBuffer("from CarLot c inner join fetch c.azProject ap where c.azProject.id in " + azProjectIdStr);
        if (carNum != "") {
            hql.append(" and c.houseNum like '%" + carNum + "%' ");
        }
        hql.append(" ORDER by ap.id,c.houseStatus,c.buildNum,c.floor,c.houseNum ");
        Map<String, Object> map = Maps.newLinkedHashMap();
        return this.findHql(hql.toString(), map).size();
    }

}
